#: --- Generic static code analisys makefile

# FIXME check for static analisys tools that may not go recursively down in every source file directory

# Some suggestions:
# - Before trying to fix bugs you can run all analisys before you start and after you made your changes like that:
#	make allanalisys ANALISYS_DIR_NOW=start -> [your stuff] ->
#	make allanalisys ANALISYS_DIR_NOW=now -> diff report/start report/now [or better diff programs]
#	Be careful as directories will be overwritten without asking. Manually delete report directories to avoid incoherent data

#- ANALISYS_DIR: Analisys report output directory (do not append "/" at the end)
ANALISYS_HOME_DIR = report
ANALISYS_DIR_NOW = "report_"$(shell date -u -Im | tr -d : | tr -d - | sed s/+0000//)"_"$(shell git branch --show-current)"_"$(shell git rev-parse HEAD)
ANALISYS_DIR = $(ANALISYS_HOME_DIR)"/"$(ANALISYS_DIR_NOW)
ANALISYS_SRC_DIR = src

#: analisysclean: delete analisys data
analisysclean:
	- rm $(ANALISYS_HOME_DIR) -rf

#: analisysdir: "Initialization" recipe: ensures an existing directory
analisysdir:
	-@ mkdir $(ANALISYS_HOME_DIR)
	-@ mkdir $(ANALISYS_DIR)
	@echo "Analysis directory: "$(ANALISYS_DIR)

#- ANALISYS_CPPCHECK_FLAGS: cppcheck flags
ANALISYS_CPPCHECK_FLAGS = --template=gcc --report-progress -j4
#: cppcheck: Static Cxx analisys
cppcheck: analisysdir
# - cppcheck --enable=all $(ANALISYS_CPPCHECK_FLAGS) . 2> $(ANALISYS_DIR)/cppcheck
	- cppcheck --enable=warning $(ANALISYS_CPPCHECK_FLAGS) . 2> $(ANALISYS_DIR)/cppcheck_warn
	- cppcheck --enable=unusedFunction $(ANALISYS_CPPCHECK_FLAGS) . 2> $(ANALISYS_DIR)/cppcheck_warn
	- cppcheck --enable=information $(ANALISYS_CPPCHECK_FLAGS) . 2> $(ANALISYS_DIR)/cppcheck_warn
	- cppcheck --enable=style $(ANALISYS_CPPCHECK_FLAGS) . 2> $(ANALISYS_DIR)/cppcheck_style
	- cppcheck --enable=missingInclude $(ANALISYS_CPPCHECK_FLAGS) . 2> $(ANALISYS_DIR)/cppcheck_incl

#: splint: Static Cxx analisys
splint: analisysdir
	- splint +posixlib -I$(ANALISYS_SRC_DIR)/* $(ANALISYS_SRC_DIR)/*.c > $(ANALISYS_DIR)/splint

#: deheader: finds unnecessary inclusions
deheader: analisysdir
	- deheader $(ANALISYS_SRC_DIR)/*.c $(ANALISYS_SRC_DIR)/*.h > $(ANALISYS_DIR)/deheader

#: cccc: overall code analisys
cccc: analisysdir
	- cccc $(ANALISYS_SRC_DIR)/* --outdir=$(ANALISYS_DIR)/cccc *

#: complex: complexity code analisys
complex: analisysdir
	- find -depth -type f -name "*.c" | complexity -ht 0 > $(ANALISYS_DIR)/comp_sources
	- find -depth -type f -name "*.h" | complexity -ht 0 > $(ANALISYS_DIR)/comp_headers

#: flawfinder: lexical source code analisys
flawfinder: analisysdir
	- flawfinder . > $(ANALISYS_DIR)/flawfinder

#: vera: probably a pain in the ass
vera: analisysdir
	- vera++ $(ANALISYS_SRC_DIR)/*.c > $(ANALISYS_DIR)/vera_c
	- vera++ $(ANALISYS_SRC_DIR)/*.h > $(ANALISYS_DIR)/vera_h

#: allanalisys: makes all analisys together
allanalisys: analisysdir cppcheck splint deheader complex cccc flawfinder vera

#: TAGS: updates tags for programs who need TAGS file to work
TAGS:
	ctags -e --language-force=C -R ./
